var arraytool = {
  /*删除数组中某一个元素*/
  removeAaary(_arr, _obj) {
    if (_arr){
      var length = _arr.length;
      for (var i = 0; i < length; i++) {
        if (_arr[i] == _obj) {
          if (i == 0) {
            _arr.shift(); //删除并返回数组的第一个元素
            return _arr;
          }
          else if (i == length - 1) {
            _arr.pop();  //删除并返回数组的最后一个元素
            return _arr;
          }
          else {
            _arr.splice(i, 1); //删除下标为i的元素
            return _arr;
          }
        }
      }
    }
    return [];
  },

  /*删除数组中某一个id相同的元素*/
  removeArraybyid(_arr,_obj){
    if (_arr){
      var length = _arr.length;
      for (var i = 0; i < length; i++) {
        if (_arr[i].id == _obj.id) {
          if (i == 0) {
            _arr.shift(); //删除并返回数组的第一个元素
            return _arr;
          }
          else if (i == length - 1) {
            _arr.pop();  //删除并返回数组的最后一个元素
            return _arr;
          }
          else {
            _arr.splice(i, 1); //删除下标为i的元素
            return _arr;
          }
        }
      }
    }
    return _arr;
  },


  /*删除数组中某一个originid相同的元素*/
  removeArraybyoriginid(_arr,_obj){
    if (_arr){
      var length = _arr.length;
      for (var i = 0; i < length; i++) {
        if (_arr[i].id == _obj.originId) {
          if (i == 0) {
            _arr.shift(); //删除并返回数组的第一个元素
            return _arr;
          }
          else if (i == length - 1) {
            _arr.pop();  //删除并返回数组的最后一个元素
            return _arr;
          }
          else {
            _arr.splice(i, 1); //删除下标为i的元素
            return _arr;
          }
        }
      }
    }
    return _arr;
  },


  /*删除数组中某一个id相同的元素*/
  removeArrayWithid(_arr,id){
    if (_arr){
      var length = _arr.length;
      for (var i = 0; i < length; i++) {
        if (_arr[i].id == id) {
          if (i == 0) {
            _arr.shift(); //删除并返回数组的第一个元素
            return _arr;
          }
          else if (i == length - 1) {
            _arr.pop();  //删除并返回数组的最后一个元素
            return _arr;
          }
          else {
            _arr.splice(i, 1); //删除下标为i的元素
            return _arr;
          }
        }
      }
    }
    return _arr;
  },

  /*删除数组中某一个id相同的元素*/
  removeArraybyRoomid(_arr,_obj){
    if (_arr){
      var length = _arr.length;
      for (var i = 0; i < length; i++) {
        if (_arr[i].roomId == _obj.roomId) {
          if (i == 0) {
            _arr.shift(); //删除并返回数组的第一个元素
            return _arr;
          }
          else if (i == length - 1) {
            _arr.pop();  //删除并返回数组的最后一个元素
            return _arr;
          }
          else {
            _arr.splice(i, 1); //删除下标为i的元素
            return _arr;
          }
        }
      }
    }
    return _arr;
  },

  /*给数组中的元素根据id 添加一个新属性并返回*/
  addArrayPropByid(arr,prop,obj,propvalue){
    if (arr){
      arr.forEach(function (value) {
        if (value.id == obj.id){
          value[prop] = propvalue
        }
      })
      return arr;
    }
    return arr;
  }

}

export default  arraytool;


let {
  LocalStream,
  MediaStreamFactory,
  StreamSourceInfo,
} = IRtc.Base;

export function isJSON(str) {
  if (typeof str == 'string') {
    try {
      var obj=JSON.parse(str);
      if(typeof obj == 'object' && obj ){
        return true;
      }else{
        return false;
      }
    } catch(e) {
      console.log('error：'+str+'!!!'+e);
      return false;
    }
  }
  console.log('It is not a string!')
}

export function dateStr(){
  var nowDate = new Date();
  var h = nowDate.getHours();
  var dateF = {
    year: nowDate.getFullYear(),
    month: nowDate.getMonth() + 1,
    date: nowDate.getDate(),
    h: h < 12 ? ('上午' + h) : ('下午' + (h-12)),
    m: nowDate.getMinutes(),
    s: nowDate.getMinutes()
  }
  return dateF.year + '/' + dateF.month + '/' + dateF.date + ' ' + dateF.h + ':' + dateF.m + ':' + dateF.s;
}


export function createmediaStream(isvideo,isaudio,resolutionoption,type){
  let audio =  isaudio?{
    deviceId:undefined,//Do not provide deviceId if source is not "mic".
    source:"mic",//Values could be "mic", "screen-cast", "file" or "mixed".
  }:undefined;

  let resolution = resolutionoption;
  let video = isvideo?{
    resolution: resolution,//The Resolution defines the size of a rectangle.
    frameRate: 15,
    deviceId: undefined,//Do not provide deviceId if source is not "camera".
    source: "camera",//values could be "camera", "screen-cast", "file" or "mixed".
  }:undefined;

  let videoSource = "camera";//Video source info. Accepted values are: "camera", "screen-cast", "file", "mixed" or undefined.
  let audioSource = "mic"; //Audio source info. Accepted values are: "mic", "screen-cast", "file", "mixed" or undefined.
  let streamSourceInfo = new StreamSourceInfo(audioSource, videoSource);

  let mediaStreamDeviceConstraints = {
    audio: audio,
    video: video,
  }

  console.log('create stream options',mediaStreamDeviceConstraints);

  return MediaStreamFactory.createMediaStream(mediaStreamDeviceConstraints).then(function (mediaStream) {
    console.log(mediaStream);
    return new LocalStream(mediaStream,streamSourceInfo,{"type":type})
  }).catch(function (error) {
    console.log(error);
    return Promise.reject(error)
  }).then(function (localstream) {
    console.log(localstream);
    return localstream;
  }).catch(function (error) {
    console.log(error);
    return Promise.reject(error)
  })
}


/*ics新版本修复screen share的bug后不需要resolution*/
export function createscreenstream(type) {
  let audio =
    {
      deviceId:undefined,//Do not provide deviceId if source is not "mic".
      source:"screen-cast",//Values could be "mic", "screen-cast", "file" or "mixed".
    }
  let resolution = {
    width:640,
    height:480
  }
  let video
    = {
    // resolution: resolution,//The Resolution defines the size of a rectangle. 不需要这个参数
     frameRate: 15,
    // deviceId: undefined,//Do not provide deviceId if source is not "camera".
     source: "screen-cast",//values could be "camera", "screen-cast", "file" or "mixed".
  }

  let videoSource = "screen-cast" //Video source info. Accepted values are: "camera", "screen-cast", "file", "mixed" or undefined.
  let audioSource = "screen-cast" //Audio source info. Accepted values are: "mic", "screen-cast", "file", "mixed" or undefined.
  let streamSourceInfo = new StreamSourceInfo(audioSource, videoSource);

  // let extensionId = localStorage.getItem('screenId').toString();////,"kaigeejmpfbncokkedpenhjhfpfcghbf"

  let mediaStreamDeviceConstraints = {
    audio: audio,
    video: video,
    // extensionId: extensionId //The ID of Chrome screen sharing extension.
  }

  console.log('screen share option',mediaStreamDeviceConstraints);

  return MediaStreamFactory.createMediaStream(mediaStreamDeviceConstraints).then(function (mediaStream) {
    console.log(mediaStream);
    return new LocalStream(mediaStream,streamSourceInfo,{"type":type})
  }).catch(function (error) {
    console.log(error);
    return Promise.reject()
  }).then(function (localstream) {
    console.log(localstream.source.video);
    console.log(localstream.source.audio);
    return localstream;
  }).catch(function (error) {
    console.log(error);
    return Promise.reject()
  })
}

export function publishlocalstream(client,stream,option) {
  return client.publish(stream,option).then(function (publication) {
    console.log("publish local success")
    console.log(publication);
    return publication;
  }).catch(function (error) {
    console.log('publish local stream failed: ', error);
    console.log(option);
  })
}

export function publishscreenstream(client,stream,option) {
  return client.publish(stream,option).then(function (publication) {
    console.log("publish screen stream success")
    console.log(publication);
    return publication;
  }).catch(function (error) {
    console.log('publish screen stream failed: ', error);
  })
}


